# encoding: utf-8
# !/usr/bin/env python
import web
import MySQLdb
from DBHelper import DBHelper


render = web.template.render('template/')

urls = (
    '/', 'index',
    '/add', 'add'
)

app = web.application(urls, globals())
db = web.database(dbn='mysql',user='root',pw='lovemin8',db='test')

class index:
    def GET(self):
        todo = db.select('todo')

        return render.index(todo)

class add:
    def POST(self):
        i = web.input()
        # n = db.insert('todo', title=i.title)
        dbHelper = DBHelper()
        print i.name
        # print i.content
        print i.markdown
        # dbHelper.execute("SELECT count(*) FROM posts")
        dbHelper.execute("INSERT INTO posts(id, uuid, title, markdown, html, img, author_id) values(123, '123-123', %s, %s,"
                         "%s,'none', '321'" % (i.name, i.markdown, i.content))
        print dbHelper.readOne("SELECT count(*) FROM posts")
        # INSERT INTO posts(id, uuid, title, markdown, html, img, author_id) values(123, '123-123', i.name, i.markdown, i.content,

        # Open Database Connection
        # db = MySQLdb.connect("localhost", "root", "lovemin8", "ghost")
        # cursor = db.cursor()
        # cursor.execute("SELECT * FROM posts")
        # data = cursor.fetchone()
        # db.close()

        raise web.seeother('/')

if __name__ == '__main__':
    app.run()
